﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DTO;
using ModuleDALCore;

namespace DAL
{
	public class SMSScheduleDAO:DALBase
	{
		public SMSScheduleDAO()
        {
            this.OpenConnect();
        }
        private DataTable dt;
        private string _SpName;

        public string SpName
        {
            get { return "SPSMSSchedule"; }
            set { _SpName = value; }
        }
		private long IDCHINHANH()
		{
			long id = 0;
			foreach (USERS u in Session.USER)
			{
				id= u.IDCONGTY;
			}
			return id;
		}
        public DataTable GetListSMS()
        {
            dt = new DataTable();
			try
			{

				dt = DBManager.Intance.ExecuteDataTable(SpName, ConvertClassToArray.GetParamName(new SMSSchedule(), null), ConvertClassToArray.GetValues(new SMSSchedule(), 0));
				return dt;
			}
			catch { return null; }
        }
		public List<SMSSchedule> GetListSMSS()
		{
			dt = new DataTable();
			List<SMSSchedule> SMS = new List<SMSSchedule>();
			try
			{

				dt = DBManager.Intance.ExecuteDataTable(SpName, ConvertClassToArray.GetParamName(new SMSSchedule(), null), ConvertClassToArray.GetValues(new SMSSchedule(), 0));
				SMS = (List<SMSSchedule>)ConvertDatatableToList.ConvertTo<SMSSchedule>(dt);
				return SMS;
			}
			catch { return null; }
		}
        public String CreatenewId()
        {
            try
            {
                return "SMS" + DBManager.Intance.ExecuteNonQuery(SpName, ConvertClassToArray.GetParamName(new SMSSchedule(), null), ConvertClassToArray.GetValues(new SMSSchedule(), 1));

            }
			catch { return null;}
        }
		public void BEGINTRANSACTION()
		{
			DBManager.Intance.BeginTransaction();
		}
		public void CLOSE()
		{
			DBManager.Intance.CloseConnection();
		}
		public void COMMIT()
		{
			DBManager.Intance.CommitTransaction();
		}
		public void ROLLBACK()
		{
			DBManager.Intance.RollbackTransaction();
		}
		public bool INSERT(SMSSchedule sms, List<CT_SMS> lct)
		{
			bool kq = true;
			long id = 0;
			try
			{
				BEGINTRANSACTION();
				id = INSERTSMS(sms);
				if (id > 0)
				{
					foreach (CT_SMS ct in lct)
					{
						ct.IDSMS = id;
						ct.ISSENT = false;
						if (!INSERTCT_SMS(ct))
						{
							kq = false;
							break;
						}
					}

				}
				else
				{
					kq = false;
				}
				if (kq)
					COMMIT();
				else
					ROLLBACK();
			}
			catch
			{
				ROLLBACK();
				kq = false;
			}
			finally 
			{
				CLOSE();			
			}

			return kq;
		}
		public bool INSERTCT_SMS(CT_SMS ct)
		{
			try
			{
				return (DBManager.Intance.ExecuteNonQueryWithTransaction("SPCT_SMS", ConvertClassToArray.GetParamName(ct, null), ConvertClassToArray.GetValues(ct, 1)) == 1);

			}
			catch { return false; }
		}
        public long INSERTSMS(SMSSchedule SMS)
        {

            try
            {
                return long.Parse((DBManager.Intance.ExecuteNonQueryWithTransaction(SpName, ConvertClassToArray.GetParamName(SMS, null), ConvertClassToArray.GetValues(SMS, 2)).ToString()));

            }
			catch { return 0; }
        }
		public bool UPDATE(SMSSchedule sms, List<CT_SMS> lct)
		{
			bool kq = true;
			try
			{
				BEGINTRANSACTION();
				if (UPDATE_SMS(sms))
				{
					foreach (CT_SMS ct in lct)
					{
						if (!DELETECT_SMS(ct))
						{
							kq = false;
							break;
						}
					}
				}
				else
				{
					kq = false;
				}
				if (kq)
				{
					COMMIT();
				}
				else
				{
					ROLLBACK();
				}
			}
			catch { ROLLBACK();
			kq = false;
			}
			return kq;
		}
        public bool UPDATE_SMS(SMSSchedule SMS)
        {

            try
            {
                return (DBManager.Intance.ExecuteNonQueryWithTransaction(SpName, ConvertClassToArray.GetParamName(SMS, null), ConvertClassToArray.GetValues(SMS, 3)) == 1);

            }
			catch { return false;}
        }
        public bool DELETE_SMS(SMSSchedule SMS)
        {

            try
            {
                return (DBManager.Intance.ExecuteNonQueryWithTransaction(SpName, ConvertClassToArray.GetParamName(SMS, null), ConvertClassToArray.GetValues(SMS, 4)) ==1);

            }
			catch { return false;}
        }
		public bool DELETE(SMSSchedule sms, List<CT_SMS> lct)
		{
			bool isok = true;
			bool kq = true;
			try
			{
				BEGINTRANSACTION();

				foreach (CT_SMS ct in lct)
				{
					if (!DELETE_CT_SMS(ct))
					{
						isok = false;
						kq = false;
						break;
					}
				}
				if (isok)
				{
					kq = DELETE_SMS(sms);
				}
				if (kq)
				{
					COMMIT();
				}
				else
				{
					ROLLBACK();
				}
			}
			catch
			{
				ROLLBACK();
				kq = false;

			}
			finally {
				CLOSE();
			}
			return kq;
		}
		public bool DELETECT_SMS(CT_SMS ct)
		{
			try
			{
				return (DBManager.Intance.ExecuteNonQueryWithTransaction("SPCT_SMS", ConvertClassToArray.GetParamName(ct, null), ConvertClassToArray.GetValues(ct, 2)) == 1);

			}
			catch { return false; }
		}
		public bool DELETE_CT_SMS(CT_SMS ct)
		{
			try
			{
				return (DBManager.Intance.ExecuteNonQueryWithTransaction("SPCT_SMS", ConvertClassToArray.GetParamName(ct, null), ConvertClassToArray.GetValues(ct, 2)) == 1);

			}
			catch { return false; }
		}
	}
}
